package com.simpd.http_helper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.http.NameValuePair;
import android.util.Log;

public class HtmlDownloader {

	private String url;
	private String pathFile;
	private ArrayList<NameValuePair> parameters;
	private String respond;

	private boolean isGet = true;
	private boolean isPost = false;

	public HtmlDownloader() {
	}

	public HtmlDownloader(String url, String filePath) {
		this.url = url;
		this.pathFile = filePath;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUrl() {
		return this.url;
	}

	public void setPathFile(String pathFile) {
		this.pathFile = pathFile;
	}

	public String getPathFile() {
		return this.pathFile;
	}

	public void isGet(boolean isGet) {
		if (isGet == true) {
			this.isPost = false;
		} else {
			this.isPost = true;
		}
		this.isGet = isGet;
	}

	public void isPost(boolean isPost) {
		if (isPost == true) {
			this.isGet = false;
		} else {
			this.isGet = true;
		}
		this.isPost = isPost;
	}

	public void setParameter(ArrayList<NameValuePair> parameters) {
		this.parameters = parameters;
	}

	public ArrayList<NameValuePair> getParameters() {
		return this.parameters;
	}
	
	public void startDownload(){
		if((this.pathFile != null || this.pathFile != "") && (this.url != null || this.url != "")){
			Thread thread = new Thread(new MyDownloadHtmlThread(this.url, this.parameters, true));
			thread.start();
		}
	}
	
	public class MyDownloadHtmlThread implements Runnable {

		private String url;
		private ArrayList<NameValuePair> parameters;
		private boolean isGet;

		public MyDownloadHtmlThread(String url,
				ArrayList<NameValuePair> parameters, boolean isGet) {
			this.url = url;
			this.parameters = parameters;
			this.isGet = isGet;
		}

		@Override
		public void run() {
			downloadHtmlUsingThread(url, parameters, isGet);

		}

	}

	private void downloadHtmlUsingThread(String url,
			ArrayList<NameValuePair> parameters, boolean isGet) {
		if (isGet == true) {
			// request with method GET
			downloadWithGet(url, parameters);
		} else {
			// request with method POST

		}
	}

	private void downloadWithGet(String url, ArrayList<NameValuePair> parameters) {
		String getParamUrl = "?";
		for (Iterator<NameValuePair> iterator = parameters.iterator(); iterator
				.hasNext();) {
			NameValuePair param = (NameValuePair) iterator.next();
			getParamUrl += param.getName() + "=" + param.getValue();
			if (iterator.hasNext()) {
				getParamUrl += "&";
			}
		}

		try {
			Log.d("[get-request]", "get request with url : \n"
					+ (url + getParamUrl));
			respond = CustomHttpClient.executeHttpGet(url + getParamUrl);
			respond = respond.toString();
			Log.d("[get-respond]", "respond result : " + respond);
			File file = new File(this.pathFile);
			String result = respond;
			Log.d("[result]", result);
			result = result.replace("/simpd/css/jquery.mobile-1.4.5.min.css",
					"../css/jquery.mobile-1.4.5.min.css");
			result = result.replace("/simpd/js/jsignature/jquery.js",
					"../js/jsignature/jquery.js");
			result = result.replace("/simpd/js/jsignature/jSignature.min.js",
					"../js/jsignature/jSignature.min.js");
			result = result.replace("/simpd/js/jquery.mobile-1.4.5.min.js",
					"../js/jquery.mobile-1.4.5.min.js");
			
			// writeData(file, result);
			
			Thread thread = new Thread(new WriteDataThread(file, result));
			thread.start();
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void writeData(File file, String text) {
		FileOutputStream fileOutputStream = null;
		try {
			file.createNewFile();
			if (file.exists()) {
				fileOutputStream = new FileOutputStream(file);
				fileOutputStream.write(text.getBytes());
			}
		} catch (IOException e) {
			Log.e("[IO-Errors]", "Error cause : " + e.getMessage());
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					Log.e("[IO-Errors]", "Error cause : " + e.getMessage());
					e.printStackTrace();
				}
			}
		}
	}
	
	private class WriteDataThread implements Runnable{
		private File file;
		private String textContent;
		
		public WriteDataThread(File file, String textContent) {
			this.file	 	 = file;
			this.textContent = textContent;
		}
		
		@Override
		public void run() {
			writeData(file, textContent);
		}
		
	}

}
